package com.phuc.game.andPuzzle.engine;

import java.util.ArrayList;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.util.MathUtils;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.Log;

import com.phuc.game.andPuzzle.PlayingActivity;
import com.phuc.game.andPuzzle.sprite.Background;
import com.phuc.game.andPuzzle.sprite.Cell;
import com.phuc.game.andPuzzle.sprite.Puzzle;

public class GamePlay {
	private static GamePlay game;
	public static float speed;
	private WelcomeScene welcomeScene;
	private boolean gameIsRunning;
	int time;
	int timePhase;
	
	Cell cellBlank;
	Cell[] cells;
	public Puzzle[] puzzles;
	public boolean isMoving;
	public static String level;
	private int point;
	
	CountDownTimer timer;
	
	public GamePlay(String level) {
		// Init data
		speed = 0.001f;
		time = 0;
		timePhase = 0;
		welcomeScene = new WelcomeScene();
		initCell();
		initPuzzle();
		resume();
		game = this;
		isMoving = false;
		point = 4;
		timer = new CountDownTimer(18000000, 90000) {
			@Override
			public void onTick(long millisUntilFinished) {
				point--;
				if (point < 1) {
					point = 1;
				}
				Log.d("phucphuc", "phucphuc " + point);
			}
			
			@Override
			public void onFinish() {
				endGame(false);
			}
		};
		timer.start();
	}
	
	private void initCell() {
		cells = new Cell[21];
		cells[0] = new Cell(0, 0, 0);
		cellBlank = cells[0];
		
		int index = 1;
		for (int y = 1; y <= 5; y++) {
			for (int x = 0; x < 4; x++) {
				cells[index] = new Cell(x * Puzzle.SIZE_W, y * Puzzle.SIZE_H, index);
				index++;
			}
		}
	}
	
	Background back;
	private void initPuzzle() {
		Background back = new Background(textureBackground);
		SpriteBackground sprBackground = new SpriteBackground(back);
//		Background background = new Background(textureBack);
		
		welcomeScene.setBackground(sprBackground);
//		welcomeScene.attachChild(background);
		
		puzzles = new Puzzle[20];
		ArrayList<Cell> listCells = new ArrayList<Cell>();
		for (int i = 1; i <= 20; i++) {
			puzzles[i - 1] = new Puzzle(i - 1, i);
			puzzles[i - 1].setCell(cells[i]);
			listCells.add(cells[i]);
		}
		
		// set random cells to Puzzle
		for (int loop = 0; loop < 10000; loop++) {
			int randTmp;
			Cell nextCell = null;
			randTmp = MathUtils.random(1, 4);
			switch (randTmp) {
			case 1://UP
				nextCell = getCell(cellBlank.getX(), cellBlank.getY() - Puzzle.SIZE_H);
				break;
			case 2:// DOWN
				nextCell = getCell(cellBlank.getX(), cellBlank.getY() + Puzzle.SIZE_H);
				break;
			case 3:// LEFT
				nextCell = getCell(cellBlank.getX() - Puzzle.SIZE_W, cellBlank.getY());
				break;
			case 4:// RIGHT
				nextCell = getCell(cellBlank.getX() + Puzzle.SIZE_W, cellBlank.getY());
				break;
			default:
				break;
			}
			
			if (nextCell != null) {
				for (Puzzle puzzle : puzzles) {
					if (puzzle.isInCell(nextCell)) {
						puzzle.setCell(cellBlank);
						cellBlank = nextCell;
					}
				}
			}
		}
		
		for (int i = 0; i < puzzles.length; i++) {
			welcomeScene.attachChild(puzzles[i]);
		}
		
		Background divide = new Background(textureDivide);
		divide.setPosition(22, 18);
		divide.setSize(432, 666);
		welcomeScene.attachChild(divide);
	}
	
	public void pause() {
		gameIsRunning = false;
	}
	
	public void resume() {
		gameIsRunning = true;
	}
	
	public void setBlankCell(Cell cell) {
		this.cellBlank = cell;
	}
	
	/**
	 * @return the cellBlank
	 */
	public Cell getCellBlank() {
		return cellBlank;
	}

	public void checkEndGame() {
		for (Puzzle puzzle : puzzles) {
			if (!puzzle.isInCorrectCell()) {
				return;
			}
		}
		endGame(true);
	}

	public void endGame(boolean isWin) {
		if (isWin) {
			PlayingActivity.getInstant().finishGame(point);
		} else {
			PlayingActivity.getInstant().finishGame(0);
		}
		timer.cancel();
	}
	
	public Scene getWelcomeScene() {
		return welcomeScene;
	}

	public static TextureCommon textureImage;
	public static TextureCommon textureImageBig;
	public static TextureCommon textureBackground;
	public static TextureCommon textureBack;
	public static TextureCommon textureDivide;
	
	public static void initTextures(Engine mEngine, Context context) {
		textureImage = new TextureCommon(context, 512, 512, "gfx/" + level + ".jpg", 0, 0, 4, 5);
		mEngine.getTextureManager().loadTexture(
				GamePlay.textureImage.getFaceTexture());
		
		textureImageBig = new TextureCommon(context, 512, 512, "gfx/" + level + ".jpg", 0, 0, 1, 1);
		mEngine.getTextureManager().loadTexture(
				GamePlay.textureImageBig.getFaceTexture());
		
		textureBackground = new TextureCommon(context, 512, 1024, "gfx/background2.jpg", 0, 0, 1, 1);
		mEngine.getTextureManager().loadTexture(
				GamePlay.textureBackground.getFaceTexture());
		
		textureBack = new TextureCommon(context, 512, 1024, "gfx/background2.jpg", 0, 0, 1, 1);
		mEngine.getTextureManager().loadTexture(
				GamePlay.textureBack.getFaceTexture());
		
		textureDivide = new TextureCommon(context, 256, 512, "gfx/divide.png", 0, 0, 1, 1);
		mEngine.getTextureManager().loadTexture(
				GamePlay.textureDivide.getFaceTexture());
	}
	
	public static GamePlay getInstant() {
		return game;
	}
	
	
	public Cell getCell(int x, int y) {
		for (Cell cell : cells) {
			if (cell.getX() == x && cell.getY() == y) {
				return cell;
			}
		}
		return null;
	}

}
